package com.whmnx.base.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RatingBar;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.R;
import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.bean.OrderDetailBean;
import com.whmnx.base.bean.ReplyBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.ImageUtil;
import com.whmnx.base.view.RoundImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * author : huang
 * date   : 2019/10/21 0021 15:06
 * version: 1.0
 * @author Administrator
 */
public class DynamicAdapter2 extends CommonRecyclerAdapter<OrderDetailBean.ProductListBean> {
    private Context context;
    private List<Boolean> alreadyLikeList = new ArrayList<>();
    private List<String> lists = new ArrayList<>();
    private ImageView view;

    private ReplyListener mReplyListener;
    private List<OrderDetailBean.CommentImageModelBean2.CommentImageModelBean> commentImageModel;
    private String trim;

    public void setReplyListener(ReplyListener replyListener) {
        mReplyListener = replyListener;
    }


    public interface ReplyListener {
      //  void commentReplyTopUser(DynamicCommentBean item, int position);

        void commentReplyUser(String item, String forumComentId, int position);

     //   void dianz(DynamicCommentBean item, int position);
    }

    public DynamicAdapter2(Context context) {

        super(context, R.layout.item_list_dynamic2);
        this.context = context;
    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, final OrderDetailBean.ProductListBean item, int position) {

        if (null != item.getCommentModel()) {
            ImageUtil.setImage((CircleImageView) helper.getView(R.id.civ_personal_head_image), item.getCommentModel().getUserInfo_Image());
            helper.setText(R.id.tv_name, (String) item.getCommentModel().getUserInfo_Name());
            helper.setText(R.id.tv_time, (String) item.getCommentModel().getDateTime());
            RatingBar ratingBar = helper.getView(R.id.ratingBar);
            ratingBar.setRating(item.getCommentModel().getComment_Praise());
            ratingBar.setClickable(false);
            GridView view = helper.getView(R.id.rv_dynamic_photos);
            //9宫格图片
            List<String> list = new ArrayList<>();
            FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(context, list);
            view.setAdapter(fansImagesAdpter);
            if (item.getCommentModel().getCommentImageModel().size() > 0) {
                commentImageModel = item.getCommentModel().getCommentImageModel();
                for (OrderDetailBean.CommentImageModelBean2.CommentImageModelBean commentImageModelBean : item.getCommentModel().getCommentImageModel()) {
                    list.add(commentImageModelBean.getImage());
                }
                fansImagesAdpter.notifyDataSetChanged();
            } else {
                fansImagesAdpter.notifyDataSetChanged();
            }

            if (item.getCommentModel().getIsReply() == 0){
                helper.setVisible(R.id.ll_input_view, View.VISIBLE);
                helper.setVisible(R.id.ll_2, View.GONE);
            }else {
                helper.setVisible(R.id.ll_input_view, View.GONE);
                helper.setVisible(R.id.ll_2, View.VISIBLE);
                helper.setText(R.id.tv_Reply_Content, (String) item.getCommentModel().Reply_Content);
            }
        }else {
            helper.setVisible(R.id.rl_top,View.GONE);
        }

        ImageUtil.setImage((RoundImageView)helper.getView(R.id.iv_goods), item.getProduct_Image());
        helper.setText(R.id.tv_goods_desc, (String) item.getProduct_Name());
        helper.setText(R.id.tv_desc, (String) item.getProduct_SPEC());
        helper.setText(R.id.tv_goods_num, (String) "x" + item.getProduct_Num());
        helper.setText(R.id.tv_moeny, (String) "￥" + item.getProduct_Price());

        final EditText etInput = helper.getView(R.id.et_input_message);
        helper.setOnClickListener(R.id.tv_send, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                trim = etInput.getText().toString().trim();
                if (TextUtils.isEmpty(etInput.getText().toString())){
                    ToastUtil.toastShortMessage("请输入回复");
                    return;
                }
                if (!SPUtils.getInstance().getString("mId").equals(SPUtils.getInstance().getString(Constants.USERID))){
                    ToastUtil.toastShortMessage("仅商家能回复");
                    return;
                }
                Map<String, Object> params = new HashMap<>();
                ReplyBean replyBean =   new ReplyBean();
                replyBean.setId(item.getCommentModel().getComment_Id());
                replyBean.setContent(trim);
                List<ReplyBean> replyBeanList = new ArrayList<>();
                replyBeanList.add(replyBean);

                params.put("orderNo", SPUtils.getInstance().getString("no"));
                params.put("replyEntity",replyBeanList);
                HttpClient.post(context, Constants.MERCHANTREPLY, params,new CallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean result) {
                        RxBus.getDefault().post(EventConstants.HUFU);
                    }
                });
            }
        });
    }
}

